package edu.ufrj.pesc.pdfsearch;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.solr.common.SolrInputDocument;
import org.apache.tika.metadata.Metadata;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;

public class PDFSearchSolr {

	Collection<SolrInputDocument> docs;
	private static SolrServer server;
	
	public PDFSearchSolr() {
		docs = new ArrayList<SolrInputDocument>();
		server = new HttpSolrServer("http://localhost/solr/collection1");
		try {
			server.deleteByQuery( "*:*" );
			server.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("deprecation")
	public void addDocumentToVector(int docId, File file, Metadata md, String content, String isbn) {
		SolrInputDocument doc = new SolrInputDocument();
		
		doc.addField("id", "docId"+docId);
		doc.addField("name", file.getName());
		doc.addField("links", file.getAbsolutePath());
		doc.addField("title", md.get(Metadata.TITLE));
		doc.addField("content", content);
		doc.addField("author", md.get(Metadata.AUTHOR));
		doc.addField("subject", md.get(Metadata.SUBJECT));
		doc.addField("isbn", isbn);
		docs.add(doc);
	}
	
	public void uploadDocuments(){
		try {
			server.add(docs);
			server.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
